package morphus; 

use strict ; use warnings ; use Exporter;

BEGIN { 	

	my ( $MyBareName  , $LibDir , $RunDir ) = () ; 
	$RunDir = '' ; 
	$0 =~ m/^(.*)(\\|\/)(.*)\.([a-z]*)/; 
	$RunDir = $1 if defined $1 ; 
	push ( @INC , $RunDir) ; 	
	#debug print join ( ' ' , @INC ) ; 

} 

use Configurator ; use Logger ; use SelfReflector ; use EnvReplicator ; 

#	anonymous hash !!!
our ( $confHolder , $objLogger , $MyBareName , $objConfigurator)= () ; 
my $hereDoc =<<LABEL1; 
; ""
#'
LABEL1



sub main {
	
	Initialize();
	$objLogger->LogMsg	(	" START MAIN " ) ; 	
	
	my @RunCommands = split('\s+' , $confHolder->{'RunCommands'} ) ; 

	foreach my $RunCommand ( @RunCommands ) {

		$objLogger->LogDebugMsg ( "should run the following \$RunCommand: $RunCommand " ) ; 
	
		ExecuteRunCommand() if ( $RunCommand eq "run" ) ; 
		ExecuteReplicateCommand() if ( $RunCommand eq "replicate" ) ; 
		
	
	
	} #eof foreach 
	
	
	$objLogger->LogMsg	(	" STOP MAIN \n\n" ) ; 
	
	

} #eof main 

sub ExecuteRunCommand {

	ReadDirExample();
	
	my $VariableToReplace='NewReplacedValue' ; 
	my $StringWithVariables = $confHolder->{'VariableWithVariables'} ; 
	my $VariableWithVariables = eval "$StringWithVariables"  ; 
	$objLogger->LogDebugMsg	(	"Test variable interpolation: " . $VariableWithVariables ) ; 
	$objLogger->LogDebugMsg	(	"Use of the here doc " . $hereDoc ) ; 
	
	
	$objLogger->LogMsg		(	"this is a simple message" ) ; 
	$objLogger->LogErrorMsg	(	"This is an error message " ) ; 
	$objLogger->LogWarningMsg	(	"This is a warning message " ) ; 
	$objLogger->LogInfoMsg	(	"This is a info message " ) ; 
	$objLogger->LogDebugMsg	(	"This is a debug message " ) ; 
	$objLogger->LogTraceMsg	(	"This is a trace message " ) ; 
	$objLogger->LogMsg	(	"using the following log file " .  "$confHolder->{'LogFile'}" ) ; 
	$objLogger->LogDebugMsg	(	"using the following Env vars \n\n" . $objConfigurator->DumpEnvVars() ) ; 

	JoinExample()	 ; 
	ForEachExample();


} #eof sub 

sub ExecuteReplicateCommand {

	my $objEnvReplicator = new EnvReplicator ( \$confHolder ) ; 
	$objEnvReplicator->Replicate(); 


} #eof sub 





sub Initialize {

	my $IniFile = "$ARGV[0]" ; 
	# create the configurator object 
	$objConfigurator = new Configurator($IniFile); 
	# get the hash having the vars 
	$confHolder = $objConfigurator->getConfHolder () ; 
	# pring the hash vars 
	print $objConfigurator->dumpIni();	
	$objLogger = new Logger (\$confHolder) ; 
	$objLogger->LogDebugMsg	(	"using the following settings" . $objConfigurator->dumpIni() ) ; 
	
	my $objSelfReflector = new SelfReflector (\$confHolder) ; 
	$objSelfReflector->ListProductFiles (); 
	$objSelfReflector->ListProductFilesRelative (); 
	
} #eof sub

sub JoinExample {

	my @list = qw ( one two tree) ;
	my $StrJoinedStrings = join ( ':' , @list ) ; 
	$objLogger = new Logger (\$confHolder) ; 
	$objLogger->LogDebugMsg	(	"\$StrJoinedStrings is $StrJoinedStrings") ; 

} #eof sub


sub ForEachExample {

	my @list = qw ( one two tree) ;
	
	$objLogger = new Logger (\$confHolder) ; 
	foreach my $word ( @list ) 		{
	
		$objLogger->LogDebugMsg	(	"\$word is $word") ; 
		$objLogger->LogDebugMsg	(	"\$_ is $_") ; 
	} #eof foreach 
	
} #eof sub



sub ReadDirExample	{

my	$objLogger = new Logger (\$confHolder) ; 
my $DirToRead = $confHolder->{'ProductVersionDir'} ; 

opendir(DIRTOREAD, $DirToRead) || die "can't DirToRead $DirToRead: $!";

$objLogger->LogDebugMsg ( "\$DirToRead is $DirToRead " ) ; 
while ( 
    defined( my $FileToProcess = readdir DIRTOREAD ) )
  {
		$objLogger->LogDebugMsg ( "\$FileToProcess is $FileToProcess" ) ; 
		my $FilePathToProcess = "$DirToRead/$FileToProcess" ; 
		$objLogger->LogDebugMsg ( "\$FilePathToProcess is $FilePathToProcess" ) ; 
	} #eof while

closedir DIRTOREAD;

} #eof sub



#Action !!!
main(); 

1 ; 

__END__


